package com.zhuiyun.pm.sys.dictionary.controller;

import com.zhuiyun.pm.config.CommonResult;
import com.zhuiyun.pm.page.MyPageHelper;
import com.zhuiyun.pm.page.PageRequest;
import com.zhuiyun.pm.page.PageResult;
import com.zhuiyun.pm.sys.dictionary.entity.DictionaryEntity;
import com.zhuiyun.pm.sys.dictionary.entity.DictionaryTypeEntity;
import com.zhuiyun.pm.sys.dictionary.mapper.DictionaryEntriesMapper;
import com.zhuiyun.pm.sys.dictionary.mapper.DictionaryMapper;
import com.zhuiyun.pm.sys.dictionary.mapper.DictionaryTypeMapper;
import com.zhuiyun.pm.sys.dictionary.service.DictionaryService;
import com.zhuiyun.pm.sys.user.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @ClassName DictionaryController
 * @Description TODO
 * @Author 胡亮
 * @Date 2023/03/14 上午 09:41
 **/
@RestController
@RequestMapping("/dictionary")
@CrossOrigin
public class DictionaryController {
    @Autowired
    DictionaryMapper dictionaryMapper;

    @Autowired
    DictionaryTypeMapper dictionaryTypeMapper;

    @Autowired
    DictionaryService dictionaryService;

    /**
     *
     * @创建人: 胡亮
     * @创建时间: 2023/03/14 下午 04:01
     * @返回值 com.zhuiyun.pm.config.CommonResult
     * @描述: 字典列表
     */
    @GetMapping("/dictionarySelectAll")
    public CommonResult dictionarySelectAll(PageRequest pageRequest){
        MyPageHelper.startPage(pageRequest);
        Example example = new Example(DictionaryEntity.class);
        Example.Criteria criteria = example.createCriteria();
        Integer dictionaryDelFlag = 0;
        criteria.andEqualTo("dictionaryDelFlag", dictionaryDelFlag);
        List<DictionaryEntity> dictionaryEntityList = dictionaryMapper.selectByExample(example);
        for (DictionaryEntity d:dictionaryEntityList) {
            if(d.getDictionaryTypeId()!=null) {
                DictionaryTypeEntity dictionaryType = dictionaryTypeMapper.selectByPrimaryKey(d.getDictionaryTypeId());
                d.setDictionaryTypeName(dictionaryType.getDictionaryTypeName());
            }
        }
        PageResult pageResult = MyPageHelper.pageInfo(dictionaryEntityList);
        return CommonResult.ok(pageResult);
    }

    /**
     *
     * @创建人: 胡亮
     * @创建时间: 2023/03/14 下午 04:02
     * @返回值 com.zhuiyun.pm.config.CommonResult
     * @描述: 字典类型列表
     */
    @GetMapping("/dictionaryTypeSelectAll")
    public CommonResult dictionaryTypeSelectAll(){
        return CommonResult.ok(dictionaryTypeMapper.selectAll());
    }

    /**
     *
     * @创建人: 胡亮
     * @创建时间: 2023/03/14 下午 07:15
     * @参数1 dictionaryEntity
     * @返回值 com.zhuiyun.pm.config.CommonResult
     * @描述: 新增字典
     */
    @PostMapping("/dictionaryInsert")
    public CommonResult dictionaryInsert(@RequestBody DictionaryEntity dictionary){
        //获取当前时间
        Date day=new Date();
        //给创建时间赋值
        dictionary.setDictionaryCreateTime(day);
        //给状态赋值(0为未删除)
//        dictionary.setDictionaryDelFlag(0);
        return CommonResult.ok(dictionaryMapper.insert(dictionary));
    }

    /**
     *
     * @创建人: 胡亮
     * @创建时间: 2023/03/14 下午 07:17
     * @参数1 dictionary
     * @返回值 com.zhuiyun.pm.config.CommonResult
     * @描述: 字典条件查(不区分大小写)
     */
    @PostMapping("/fuzzySelectAll")
    public CommonResult fuzzySelectAll(@RequestBody DictionaryEntity dictionary){
        List<DictionaryEntity> dictionaryEntityList = dictionaryService.fuzzySelectAll(dictionary);
        for (DictionaryEntity d:dictionaryEntityList) {
            DictionaryTypeEntity dictionaryType = dictionaryTypeMapper.selectByPrimaryKey(d.getDictionaryTypeId());
            d.setDictionaryTypeName(dictionaryType.getDictionaryTypeName());
        }
        return CommonResult.ok(dictionaryEntityList);
    }

    /**
     *
     * @创建人: 胡亮
     * @创建时间: 2023/03/14 下午 08:13
     * @参数1 dictionary
     * @返回值 com.zhuiyun.pm.config.CommonResult
     * @描述: 修改字典
     */
    @PostMapping("/dictionaryUpdate")
    public CommonResult dictionaryUpdate(@RequestBody DictionaryEntity dictionary){
        //获取当前时间
        Date day=new Date();
        //给修改时间赋值
        dictionary.setDictionaryUpdateTime(day);
        //给状态赋值(0为未删除)
        dictionary.setDictionaryDelFlag(0);
        return CommonResult.ok(dictionaryMapper.updateByPrimaryKey(dictionary));
    }

    /**
     *
     * @创建人: 胡亮
     * @创建时间: 2023/03/14 下午 08:14
     * @参数1 id
     * @返回值 com.zhuiyun.pm.config.CommonResult
     * @描述: 删除字典(逻辑删除)
     */
    @PostMapping("/dictionaryDel/{id}")
    public CommonResult dictionaryDel(@PathVariable Integer id){
        return CommonResult.ok(dictionaryService.dictionaryDel(id));
    }
}
